package com.example.fw;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;


public class HibernateHelper extends HelperBase {

	public HibernateHelper(ApplicationManager manager) {
	  super(manager);
	}

	public void deleteUser(String login) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		try {
			String hql = "delete from User where login = :login";
			session.createQuery(hql).setString("login", login).executeUpdate();
		} finally {
          trans.commit();
		}
	}
	
	public boolean userExists(String login) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		try {
			String hql = "from User where login = :login";
			if(session.createQuery(hql).setString("login", login).list().isEmpty()) {
				return false;
			} else {
				return true;
			}
		} finally {
          trans.commit();
		}	
	}
	
}
